package net.esj.basic.core.view.filter;

import java.io.IOException;
import java.util.Locale;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class I18NFilter implements Filter {

	public static final String STRUTS_I18N_KEY  = "WW_TRANS_I18N_LOCALE";
	
	@Override
	public void destroy() {

	}

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain arg2) throws IOException, ServletException {
		String local=arg0.getParameter("local"); 
		if(local!=null){ 
			String loc[]=local.split("_"); 
			Locale locale=new Locale(loc[0],loc[1]); 
			((HttpServletRequest)arg0).getSession().setAttribute(STRUTS_I18N_KEY, locale); 
		} 
		arg2.doFilter(arg0, arg1); 
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {

	}

}
